var EXE_RESULT = ['成功', '失败'];
var Page={
	init: function() {
		
		apps.request({
			url: urls.ctx + '/task/init',
			success: function(data) {
				console.log(data);
				if(data) {
					if(data.taskGroupList) {
						var groupSelect = '';
						$.each(data.taskGroupList, function(k, v) {
							groupSelect += '<option value="' + v + '">' + v + '</option>';
						});
						$('#s-taskgroup').append(groupSelect);
					}
					
					Page.search();
				}
			}
		});
	},
	search: function() {
		var pageNo = $('#page-demo1').attr('data-page');
		var param = {
			pageNo: pageNo ? pageNo : 1,
			pageSize: 10,
		};
		var taskName = $('#s-taskname').val();
		if(taskName) {
			param.taskName = taskName;
		}
		var taskGroup = $('#s-taskgroup').val();
		if(taskGroup) {
			param.taskGroup = taskGroup;
		}
		var exeLong = $('#s-exelong').val();
		if(exeLong) {
			param.exeLong = exeLong;
		}
		var exeResult = $('#s-exeresult').val();
		if(exeResult) {
			param.exeResult = exeResult;
		}
		apps.request({
			url: urls.ctx + '/tasklog/query',
			data: param,
			success: function(data) {
				console.log(data);
				if(data) {
					
					if(data.data) {
						var tabltHtml = '';
						$.each(data.data, function(k, v) {
							tabltHtml += '<tr data-id="' + v.id + '">';
							tabltHtml += '<td>' + v.id + '</td>';
							tabltHtml += '<td>' + v.task_name + '</td>';
							tabltHtml += '<td>' + v.task_group + '</td>';
							tabltHtml += '<td>' + v.start_time + '</td>';
							tabltHtml += '<td>' + v.end_time + '</td>';
							tabltHtml += '<td>' + v.exe_time_long + '</td>';
							tabltHtml += '<td>' + EXE_RESULT[v.exe_result] + '</td>';
							tabltHtml += '</tr>';
						});
						$('#tbody-data').html(tabltHtml);
						
						$('.datalist-edit').unbind().bind('click', function() {
							var taskId = $(this).parents('tr').attr('data-id');
							if(taskId) {
								Page.edit(taskId);
							} else {
								
							}
						});
					}
					
					
					$("#demo1").jqPaginator({
						totalPages: data.totalpage,
						visiblePages: data.pageSize,
						currentPage: data.page,
						first: '<li class="first"><a href="javascript:void(0);">First<\/a><\/li>',
						prev: '<li class="prev"><a href="javascript:void(0);">Previous<\/a><\/li>',
						next: '<li class="next"><a href="javascript:void(0);">Next<\/a><\/li>',
						last: '<li class="last"><a href="javascript:void(0);">Last<\/a><\/li>',
						page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
						onPageChange : function(num, type) {
							console.log(type + '：' + num);
							if (type == 'change') {
								$("#page-demo1").attr('data-page',num);
								Page.search();
							}
						}
					  })
				}
			}
		});
		
		
	},
	
};

$(document).ready(function(){
	
	//列表查询
	$('#s-submit').click(function() {
		Page.search();
	});

	
	Page.init();
	
});


